<?php
require_once("includes/pagetop.php");
if(!isset($_SESSION['Allyfolio_User_ID']))
{
    redirect("index.php");
}

if(!isset($_POST['event_id']) && !($_POST['event_id']==""))
{
    redirect("events.php");
}

$eventinfo=$pdodbobj->fetch_array($pdodbobj->query("select af_events.*,Allyfolio_User_ID,Full_Name from af_events,af_users where af_users.User_ID=af_events.Created_By and Event_ID=:eventid ",array("eventid"=>base64_decode($_POST['event_id']))));

if(count($eventinfo)==0)
{
    redirect("events.php");
}

if(isset($_POST['actiontype']) && $_POST['actiontype']=="SaveEvent" )
{
    $eventarr=$_POST['Event'];
    $eventarr['Friends_ID']=str_ireplace("[","",$eventarr['Friends_ID']);
    $eventarr['Friends_ID']=str_ireplace("]","",$eventarr['Friends_ID']);
    $eventarr['Friends_ID']=str_ireplace('"',"",$eventarr['Friends_ID']);
    
    $friendarr=explode(",",$eventarr['Friends_ID']);
    
    $userids="";
    if(count($friendarr)>0)
    {
        for($i=0;$i<count($friendarr);$i++)
        {
            $condarr=array("recid"=>base64_decode($_POST['event_id']),"toid"=>$friendarr[$i]);
            $eventsinfo=$pdodbobj->fetch_array($pdodbobj->query("select Inbox_ID from af_user_inbox where Record_ID=:recid and To_User_ID=:toid AND Module_From='Event'",$condarr));
            if(count($eventsinfo)==0)
            {
                $insertarr=array();
                $insertarr['Record_ID']=base64_decode($_POST['event_id']);
                $insertarr['Module_From']="Event";
                $insertarr['From_User_ID']=$_SESSION['Allyfolio_User_ID'];
                $insertarr['To_User_ID']=$friendarr[$i];
                $insertarr['Message']=$eventinfo[0]['Inbox_Message'];
                $insertarr['Message_Date']=time();
                $pdodbobj->insertData("af_user_inbox",$insertarr);
                $userids.=$friendarr[$i].",";
            }
            
        }
    }
    if($userids!="")
    {
        $userids=substr($userids,0,strlen($userids)-1);
        if($eventinfo[0]['Friends_ID']=="")
        {
            $userids=$userids;    
        }
        else
        {
            $userids=$eventinfo[0]['Friends_ID'].",".$userids;
        }
        
        $pdodbobj->updateData("af_events",array("Friends_ID"=>$userids),array("Event_ID"=>base64_decode($_POST['event_id'])));
    }
    if(count($friendarr)>0)
    $msg="Event Invitation Sent successfully";
    $msgtype="Success";
}

$eventinfo=$pdodbobj->fetch_array($pdodbobj->query("select af_events.*,Allyfolio_User_ID,Full_Name from af_events,af_users where af_users.User_ID=af_events.Created_By and Event_ID=:eventid ",array("eventid"=>base64_decode($_POST['event_id']))));

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>AllyFolio | Entertainments </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />
<script src="js/general.js"></script>
<script src="js/functions.js"></script>

<link rel="stylesheet" href="bin/magicsuggest-1.3.1-min.css">
<script type="text/javascript" src="bin/jquery-1.8.3-min.js"></script>
<script type="text/javascript" src="bin/magicsuggest-1.3.1-min.js"></script>


<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>	

<script src="js/jquery_raty.js"></script>

<script src="js/jquery_1.9.0.min.js" type="text/javascript"></script>
<script src="assets/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript" src="assets/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<!-- BEGIN PAGE LEVEL SCRIPTS -->
<script src="assets/scripts/core/app.js"></script>


<script type="text/javascript">
$(function() {
	$('#popupDatepicker').datepick();
	$('#inlineDatepicker').datepick({onSelect: showDate});
});

function showDate(date) {
	alert('The date chosen is ' + date);
}
</script>

<link type="text/css" href="css/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/jquery.datepick.js"></script>
</head>

<body>


</head>

<body>
<div id="main_wrapper">
<?php
    $pageheading="Event Details";
    require_once("includes/header.php");
    
    $privacytypes=$pdodbobj->fetch_array($pdodbobj->query("select * from af_privacy_types"));
    
   ?>
    <!--=================Body Contain Start=======================-->
    <div class="wrapper">
    	<div class="header">
        		<div class="contain-wrap" style="margin-top:30px;">
 		<div class="new_inb3">
			<form name="eventform" method="post">
                <ul>
                    <center>
    				<h3>Event Details</h3>
    				
                    </center>
                    <div class="createEvent" id="createeventdiv" >
                    
                    <link href="assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
                    <link href="assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
                    
                    <link rel="stylesheet" type="text/css" href="assets/plugins/bootstrap-datetimepicker/css/datetimepicker.css"/>
    
    
                        <label><span>Name</span> : <?php if(isset($eventinfo[0]['Event_Name'])){ echo $eventinfo[0]['Event_Name']; } ?> </label>
                        <label><span>Detail</span> : <?php if(isset($eventinfo[0]['Detail'])){ echo $eventinfo[0]['Detail']; } ?></label>
                        <label><span>Where</span> : <?php if(isset($eventinfo[0]['Event_Place'])){ echo $eventinfo[0]['Event_Place']; } ?></label>
                        <label><span>When</span> :  
                            <?php if(isset($eventinfo[0]['Event_Date']) && $eventinfo[0]['Event_Date']>0){ echo date("d-M-Y h:i:s A",$eventinfo[0]['Event_Date']); } ?>
                        </label>
                        <?php if(isset($eventinfo[0]['Privacy_Type_ID']) && $eventinfo[0]['Privacy_Type_ID']==1) { ?>
                        <label id="guestinviteid" ><span>Guest can Invite friend</span> : <?php if(isset($eventinfo[0]['Guest_Invite']) ){ if($eventinfo[0]['Guest_Invite']==1){ echo "Yes"; }else{ echo "No"; }}  ?>
                        </label>
                        <?php
                        }
                        ?>
                        <?php if(isset($eventinfo[0]['Privacy_Type_ID']) && $eventinfo[0]['Privacy_Type_ID']==1) { ?>
                        <label id="addfriend"><span>Add Friend to Invite</span> <input type="text" id="usernames" name="Event[Friends_ID]" placeholder="Type Name" style="margin-left:189px;" /></label>
                        <?php
                        }
                        ?>
                        <p style="margin-top:40px; text-align:left;"><span>&nbsp;</span> 
                        <input type="submit" value="Invite Friends" style="margin-right:40px;" onclick="return checkvalidation();" /> <input type="submit" value="Back"  style="margin-right:40px;" onclick="document.eventform.action='events.php';" /> </p>
                        
                    </div>
    			</ul>
                <input type="hidden" id="event_id" name="event_id" value="<?php if(isset($_POST['event_id'])){ echo $_POST['event_id']; } ?>" />
                <input type="hidden" id="actiontype" name="actiontype" />
	         </form>
		</div>
	</div>
            
            <div class="clearfix"></div>
            </div>
            </div>
            
            
		</div>
    </div>
    
    <div class="clearfix"></div>
    <!--=================Body Contain end=======================-->
    </div>
    
    <?php
    require_once("includes/footer.php");
?>
<script type="text/javascript">
//<![CDATA[     
var userfriendids="";
        function checkvalidation()
        {
            if(trim(userfriendids)=="" && trim(userfriendids)!="[]")
            {
                alertify.alert('Please enter the specific users who are invited');
                return false;
            }
            {
                document.getElementById('actiontype').value="SaveEvent";
                return true;
            }    
        }   
        
        
        jQuery(document).ready(function() {   
            
          
            
            $(document).ready(function() {
                $.noConflict();
                
                var ms5 = $('#usernames').magicSuggest({
                    width: 300,
                    value: '',
                    id: 'userfriendids',
                    data: 'ajax_search/suggestusers.php' 
                });
                
                 $(ms5).on('selectionchange', function(){
                      userfriendids=JSON.stringify(this.getValue());
                    });
            });
            
            
        });
        
        <?php
        if(isset($msg) && $msg!="")
        {
            ?>
            alertify.alert("<?php echo $msg; ?>");
            <?php
            
        }
        ?>
//]]>
</script>